<!DOCTYPE html>
<html ng-app="app">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title></title>
    <link rel="icon" type="image/x-icon" href="" />
    <link rel="stylesheet" href="bower_components/materialize/dist/css/materialize.min.css" />
    <link rel="stylesheet" type="text/css" href="page.css">
    <script src="lib/angular-1.5.5/angular.js"></script>
    
</head>
<!--[if lte IE 8]>
<body class="lower-ie">
<img src="http://qbmf.trenddata.cn/assets/img/forie.png" alt="">
<![endif]-->
<!--[if (gt IE 8)|!(IE)]><body></body><![endif]-->
<div class="center-vertical bg-black" ng-keyup="vm.Enter($event)" ng-class="{'pp':vm.user.channel==3,'qzt': vm.user.channel==4}" ng-controller="loginCtr as vm">
    <div class="navbar-fixed">
        <nav>
            <div class="nav-wrapper">
                <a href="javascript:;" class="brand-logo left">
                    <img ng-src="{{vm.user.brand}}" alt="logo">
                </a>
                <ul id="nav-mobile" class="right">
                    <li><a href="forget.html">找回密码</a></li>
                    <li><a href="reg.html">注册</a></li>
                    <li class="active"><a href="login.html">登录</a></li>
                </ul>
            </div>
        </nav>
    </div>
    <div class="center-content">
        <div class="container">
            <div class="row">
                <div class="col m8 hide-on-small-only">
                    <div class="pic">
                        <img ng-src="{{vm.user.logPic}}" class="w h" />
                    </div>
                </div>
                <form class="col s12 m4 form-box">
                    <div id="login-form" class="content-box">
                        <div class="row content-box-wrapper">
                            <h1 class="title">登录</h1>
                            <div class="divider mb8"></div>
                            <div class="input-field col s12">
                                <input id="username" type="text" class="validate" name="nick" ng-model="vm.user.name" />
                                <label for="username" class="active">用户名</label>
                            </div>
                            <div class="input-field col s12">
                                <input id="password" type="password" class="validate" name="password" ng-class="{'invalid': vm.error.error}" ng-model="vm.user.password" />
                                <label for="password" class="active" data-error="{{vm.error.info}}">密码</label>
                            </div>
                            <div class="col s12 clearfix">
                                <input type="checkbox" id="user-remember" class="filled-in" checked="checked" ng-model="vm.user.isRemember" />
                                <label for="user-remember">记住密码</label>
                                <a href="forget.html" class="ml20 hover-underline right">忘记密码？</a>
                            </div>
                            <div class="col s12">
                                <button class="waves-effect waves-light btn col s12 hoverable" ng-click="vm.user.submit()" style="">登 录</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
(function() {

    angular.module('app', []).controller('loginCtr', loginCtr);

    loginCtr.$inject = ['$http', '$q', '$timeout'];

    function loginCtr($http, $q, $timeout) {
        var vm = this;

        vm.error = {
            error: false,
            info: '密码错误'
        };

        vm.user = {
            isRemember: false,
            brand: '',
            logPic: '',
            channel: 0,
            name: '',
            password: '',
            submit: _submit
        }


        init();



        function init() {
            if (window.location.href.indexOf('shop.630game.com') != -1) {

                document.title = "电商先生";

                document.querySelector('link[rel=icon]').href = "http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/icon.png";

                vm.user.brand="./img/dsxs/icon.png";

                vm.user.logPic = 'http://qbmf.trenddata.cn/assets/img/text.png';

                vm.user.channel = 2;
            } else if (window.location.href.indexOf('esm.fenxiao8.com') != -1) {

                document.title = "品谱大数据";

                document.querySelector('link[rel=icon]').href = "http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.brand="http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.channel = 3;

                vm.user.logPic = 'http://www.hzbigdate.com/assets/img/pingpu-text.jpg';

            } else if (window.location.href.indexOf('hzbigdate.com') != -1) {

                document.title = "品谱大数据";

                document.querySelector('link[rel=icon]').href = "http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.brand="http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.channel = 3;

                vm.user.logPic = 'http://www.hzbigdate.com/assets/img/pingpu-text.jpg';

            } else if (window.location.href.indexOf('qzt.sinoac.org') != -1) {

                document.title = "企智通";

                document.querySelector('link[rel=icon]').href = "http://qzt.sinoac.org/assets/img/qzt/qzt_ico.png";

                vm.user.brand = "http://qzt.sinoac.org/assets/img/qzt/qzt_brand.jpg";

                vm.user.channel = 4;

                vm.user.logPic = 'http://qzt.sinoac.org/assets/img/qzt/banner.png';

            } else {
                document.title = "情报魔方";
                document.querySelector('link[rel=icon]').href = 'http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/favicon.ico';
              
      

                vm.user.brand="./img/logo.png";

                vm.user.logPic = 'http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/slogan.png';

                vm.user.channel = 0;
            }



            var user = window.localStorage.getItem('user');


            if (user) {
                var parse;
                try {
                    parse = JSON.parse(user);
                } catch (oException) {
                    console.log(item);
                    console.log(oException.name);

                }
                
                angular.extend(vm.user, parse);
            }

        }

        function remeber() {

            if (vm.user.isRemember) {
                //需要保存的
                var json = {
                    isRemember: vm.user.isRemember,
                    name: vm.user.name,
                    password: vm.user.password
                };
                window.localStorage.setItem('user', JSON.stringify(json));
            } else {
                window.localStorage.removeItem('user');
            }

        }



        function _submit() {

            if (this.name == '' || this.password == "") {
                return;
            }

            _login(vm.user).then(function(resp) {


                remeber();
                vm.error.error = false;

                window.location.href = 'main.html';

            }, function(err) {
                console.log(err);
                vm.error.error = true;
                vm.error.info = err.msg;
            });

        }

        function _login(user) {

            var defer = $q.defer();


            //  $http({
            //     method: 'post',
            //     url: '/qbmf/signIn',
            //     data: {
            //         nick: user.name,
            //         password: user.password
            //     },
            //     headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            //     transformRequest: function(obj) {
            //         var str = [];
            //         for (var p in obj) {
            //             str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            //         }
            //         return str.join("&");
            //     }
            // }).success(function(resp, status, headers, config) {
            //     console.log(headers);
            //     $cookies.put('token',headers['x-token']);
            //     if (resp.code === 0) {
            //         defer.resolve(resp);
            //     } else {
            //         defer.reject(resp);
            //     }
            // }, function(err, status, headers, config) {
            //     console.log(headers);
            //     var resp = resp.data;

            //     defer.reject(err);
            // });

            $http({
                method: 'post',
                url: '/qbmf/signIn',
                data: {
                    nick: user.name,
                    password: user.password
                },
                headers: {
                    'Accept': 'application/json, text/javascript, */*',
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                transformRequest: function(obj) {
                    var str = [];
                    for (var p in obj) {
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                }
            }).success(function(resp) {

                if (resp.code === 0) {

                    defer.resolve(resp);

                } else {

                    defer.reject(resp)
                }
            });

            return defer.promise;
        }


    }


})();
</script>
</body>

</html>
